package com.crayfish.quduo.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseActivity;
import com.crayfish.quduo.commom.Params;
import com.crayfish.quduo.model.JokeItemBean;
import com.crayfish.quduo.model.Test;
import com.crayfish.quduo.ui.adapter.EndlessRecyclerOnScrollListener;
import com.crayfish.quduo.ui.adapter.FooterHolder;
import com.crayfish.quduo.ui.adapter.JokeListAdapter;
import com.crayfish.quduo.utils.DBManager;
import com.crayfish.quduo.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：crayfish(徐杰)
 * 描述：
 * Created by crayfish on 2017/3/31.
 */
public class HistoryActivity extends BaseActivity{
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private Boolean isRefresh = false;
    JokeListAdapter jokeListAdapter;
    List<Test> list = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list;
    }

    @Override
    protected void initEventAndData() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        swipeRefreshLayout.setColorSchemeColors(Color.BLUE,Color.GREEN,Color.YELLOW,Color.RED);
        swipeRefreshLayout.setDistanceToTriggerSync(300);
        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //检查是否处于刷新状态
                if (!isRefresh) {
                    isRefresh = true;
                    //模拟加载网络数据，这里设置4秒，正好能看到4色进度条
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            //显示或隐藏刷新进度条
                            swipeRefreshLayout.setRefreshing(false);
                            //修改adapter的数据
                            getData();
                            isRefresh = false;
                        }
                    }, 3000);
                }
            }
        });
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new RecycleViewDivider(this,LinearLayoutManager.VERTICAL));
        recyclerView.setHasFixedSize(true);//固定高度提高性能
        jokeListAdapter = new JokeListAdapter(this);
        jokeListAdapter.setOnRecyclerViewItemClickListener(new JokeListAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, JokeItemBean.Item item) {
                Intent intent = new Intent(HistoryActivity.this, ImageActivity.class);
                intent.putExtra(Params.JOKE_ITEM,item);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(jokeListAdapter);//替换Adapter
        getData();
    }

    protected void setState() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                (HistoryActivity.this).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //改变底部bottom的样式
                        if (jokeListAdapter != null && jokeListAdapter.footerHolder != null) {
                            jokeListAdapter.footerHolder.setStatus(FooterHolder.TheEnd);
                        }
                    }
                });
            }
        },3000);

    }

    private void getData(){
        List<JokeItemBean.Item> items = new ArrayList<>();
        list =  DBManager.getInstance(this).listAll();
        for (Test test : list){
            items.add(Test.toItem(test));
        }
        jokeListAdapter.clear();
        jokeListAdapter.addAll(items);
        setState();
    }
}
